package net.core.service.filter;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


public class SetCharacterEncodingFilter implements Filter {
	
	protected String encoding = null;
	@SuppressWarnings("unchecked")
	protected List pathList = new ArrayList();
	@SuppressWarnings("unchecked")
	protected Map encodingMap = new HashMap();
	protected FilterConfig filterConfig = null;
	protected boolean ignore = true;

	@SuppressWarnings("unchecked")
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		this.encoding = filterConfig.getInitParameter("encoding");
		if (this.encoding != null) {
			String[] encodings = this.encoding.split(";");
			if (encodings != null) {
				for (int i = 0; i < encodings.length; ++i) {
					String[] encodingOne = encodings[i].split(":");
					if ((encodingOne != null) && (encodingOne.length > 0)) {
						if (encodingOne.length == 1) {
							addPath("/");
							this.encodingMap.put("/", encodingOne[0]);
						} else {
							addPath(encodingOne[0]);
							this.encodingMap
									.put(encodingOne[0], encodingOne[1]);
						}
					}
				}
			}
		}
		String value = filterConfig.getInitParameter("ignore");
		if (value == null)
			this.ignore = true;
		else if (value.equalsIgnoreCase("true"))
			this.ignore = true;
		else if (value.equalsIgnoreCase("yes"))
			this.ignore = true;
		else
			this.ignore = false;
	}

	@SuppressWarnings("unchecked")
	private void addPath(String path) {
		boolean isAdd = false;
		int iSize = this.pathList.size();
		for (int i = 0; i < iSize; ++i) {
			String str = (String) this.pathList.get(i);
			if (path.length() >= str.length()) {
				this.pathList.add(i, path);
				isAdd = true;
			}
		}
		if (!isAdd)
			this.pathList.add(path);
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		if ((this.ignore) || (request.getCharacterEncoding() == null)) {
			String encoding = selectEncoding(request);
			if (encoding != null) {
				request.setCharacterEncoding(encoding);
				response.setCharacterEncoding(encoding);
			}
		}
		chain.doFilter(request, response);
	}

	protected String selectEncoding(ServletRequest request) {
		String url = ((HttpServletRequest) request).getServletPath();
		int iSize = this.pathList.size();
		for (int i = 0; i < iSize; ++i) {
			String str = (String) this.pathList.get(i);
			if (url.startsWith(str)) {
				return (String) this.encodingMap.get(str);
			}
		}
		return null;
	}

	public void destroy() {
		this.encoding = null;
		this.filterConfig = null;
	}
}
